package com.xframe.domain.views.builder;

import cn.hutool.json.JSONUtil;
import com.xframe.domain.emnus.Constant;
import com.xframe.domain.emnus.PublicFieldEnum;
import com.xframe.domain.role.IUserCredentials;
import com.xframe.domain.sys.SysModel;
import com.xframe.domain.sys.SysModelAttr;
import com.xframe.domain.views.ui.*;
import com.xframe.xdal.core.annotation.XGridTableRef;
import com.xframe.xdal.core.model.mapping.IColumnMapping;
import com.xframe.xdal.core.model.mapping.IRelation;
import com.xframe.xdal.core.model.mapping.ITableInfo;
import com.xframe.xdal.core.model.mapping.TableInfoBuilder;
import com.xframe.xdal.core.util.StringUtil;
import com.xframe.xdal.core.util.XReflect;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 生成实体的查看配置
 * @ClassName: ViewUIModelBuilder
 * @Author: xufeng
 * @CreateTime: 2023/12/06
 **/
@Log4j2
public class ViewUIModelBuilder extends UIModelBuilder{

    static final String VIEW_NAME = "detailView";
    static final String CONFIG_TYPE = "detail";
    static final String RENDER_CLASS = "xView";

    @Override
    public String getViewName() {
        return VIEW_NAME;
    }

    @Override
    public String getConfigType() {
        return CONFIG_TYPE;
    }

    @Override
    public String getRenderClass() {
        return RENDER_CLASS;
    }

    @Override
    public String setSysModelViewConfigContent(SysModel sysModel, IUserCredentials userCredentials) {

        List<SysModelAttr> sysModelAttrList = sysModel.getModelAttrList();
        DynamicBillView dynamicBillView = new DynamicBillView();
        dynamicBillView.setTitle(DynamicBill.NAME_SIGE_TITLE);
        dynamicBillView.setModelCode(sysModel.getModelCode());
        int indexNo = 0;
        List<DetailViewUIModel> detailViewUIModelList = new ArrayList<>();
        for(SysModelAttr modelAttr : sysModelAttrList){
            indexNo = indexNo + 10;
            DetailViewUIModel viewUIModel = new DetailViewUIModel();
            viewUIModel.setAttrName(modelAttr.getAttrName());
            viewUIModel.setLabel(TableUIModel.NAME_SIGE + modelAttr.getAttrName());
            viewUIModel.setRemark(modelAttr.getDesc());
            viewUIModel.setComponentName(UIType.LABEL.getComponentName());
            viewUIModel.setSortNo(modelAttr.getSortNo());
            viewUIModel.setProp(modelAttr.getAttrName());
            viewUIModel.setShow(true);
            viewUIModel.setSortFlag(true);
            if(modelAttr.getPrimaryKeyFlag() || modelAttr.getAttrName().equals(PublicFieldEnum.VERSION.getValue())){
                viewUIModel.setComponentName(UIType.NONE.getComponentName());
                viewUIModel.setShow(false);
                viewUIModel.setSortFlag(false);
            }
            // 判断是不是引用字段
            IRelation relation = findRefAttr(modelAttr,sysModel);
            if(relation != null){
                viewUIModel.setComponentName(UIType.NONE.getComponentName());
                viewUIModel.setShow(false);
                viewUIModel.setSortFlag(false);
                indexNo = builderRefTableUIModel(relation,detailViewUIModelList,indexNo);
            }
            if(modelAttr.getAttrName().equals(PublicFieldEnum.DEL_FLAG.getValue())){
                viewUIModel.setFilter(FilterEnum.STATUS_FILTER.getName());
                viewUIModel.setFilterArgs(StringUtil.EMPTY);
            }
            detailViewUIModelList.add(viewUIModel);
        }
        dynamicBillView.setElements(detailViewUIModelList);
        dynamicBillView.setConfigType(getConfigType());
        String json = JSONUtil.toJsonStr(dynamicBillView);
        return json;
    }


    private int builderRefTableUIModel(IRelation relation,List<DetailViewUIModel> viewUIModelList,int indexNo){
        SysModel sysModel = SysModel.getInstance(relation.getDataType().getName());
        Map<String, XGridTableRef> map = XReflect.getFieldAnnotationMap(relation.getDataType(), XGridTableRef.class);
        Integer indexSortNo = indexNo;
        if(sysModel != null) {
            // 找出所有的非公有字段
            List<SysModelAttr> sysModelAttrList = SysModelAttr.findNoPublicAttr(sysModel.getModelAttrList());
            for (SysModelAttr modelAttr : sysModelAttrList) {
                if(map.containsKey(modelAttr.getAttrName())) {
                    XGridTableRef gridTableRef = map.get(modelAttr.getAttrName());
                    indexSortNo = indexSortNo + 10;
                    DetailViewUIModel detailViewUIModel = new DetailViewUIModel();
                    detailViewUIModel.setAttrName(relation.getAs() + "." + modelAttr.getAttrName());
                    detailViewUIModel.setLabel(TableUIModel.NAME_SIGE + relation.getAs() + "." + modelAttr.getAttrName());
                    detailViewUIModel.setRemark(modelAttr.getDesc());
                    detailViewUIModel.setComponentName(UIType.LABEL.getComponentName());
                    detailViewUIModel.setSortNo(indexSortNo);
                    detailViewUIModel.setProp(relation.getAs() + "." + modelAttr.getAttrName());
                    detailViewUIModel.setShow(true);
                    detailViewUIModel.setSortFlag(false);
                    if (modelAttr.getPrimaryKeyFlag() || modelAttr.getAttrName().equals(PublicFieldEnum.VERSION.getValue())) {
                        detailViewUIModel.setComponentName(UIType.NONE.getComponentName());
                        detailViewUIModel.setShow(false);
                        detailViewUIModel.setSortFlag(false);
                    }
                    if(StringUtil.isNotEmpty(gridTableRef.filter())) {
                        detailViewUIModel.setFilter(gridTableRef.filter());
                    }
                    viewUIModelList.add(detailViewUIModel);
                }
            }
        }else {
            ITableInfo tableInfo = TableInfoBuilder.BuildTableInfoByAnnotation(relation.getDataType());

            for (IColumnMapping modelAttr : tableInfo.getColumnMappingList()) {
                if(map.containsKey(modelAttr.getFieldName())) {
                    XGridTableRef gridTableRef = map.get(modelAttr.getFieldName());
                    indexSortNo = indexSortNo + 10;
                    DetailViewUIModel detailViewUIModel = new DetailViewUIModel();
                    detailViewUIModel.setAttrName(relation.getAs() + "." + modelAttr.getFieldName());
                    detailViewUIModel.setLabel(TableUIModel.NAME_SIGE + relation.getAs() + "." + modelAttr.getFieldName());
                    detailViewUIModel.setRemark(modelAttr.getDesc());
                    detailViewUIModel.setComponentName(UIType.LABEL.getComponentName());
                    detailViewUIModel.setSortNo(indexSortNo);
                    detailViewUIModel.setProp(relation.getAs() + "." + modelAttr.getFieldName());
                    detailViewUIModel.setShow(true);
                    detailViewUIModel.setSortFlag(false);
                    if (modelAttr.getPrimaryKeyFlag() || modelAttr.getFieldName().equals(PublicFieldEnum.VERSION.getValue())) {
                        detailViewUIModel.setComponentName(UIType.NONE.getComponentName());
                        detailViewUIModel.setShow(false);
                        detailViewUIModel.setSortFlag(false);
                    }
                    if(StringUtil.isNotEmpty(gridTableRef.filter())) {
                        detailViewUIModel.setFilter(gridTableRef.filter());
                    }
                    viewUIModelList.add(detailViewUIModel);
                }
            }
        }
        return indexSortNo;
    }
}
